package org.jeecg.modules.task.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.checkerframework.checker.units.qual.A;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.activity.entity.ActivityInfo;
import org.jeecg.modules.activity.service.IActivityInfoService;
import org.jeecg.modules.clues.entity.Clues;
import org.jeecg.modules.clues.service.ICluesService;
import org.jeecg.modules.content.entity.ContentInfo;
import org.jeecg.modules.content.service.IConContentVersionService;
import org.jeecg.modules.content.service.IContentInfoService;
import org.jeecg.modules.engine.service.IWorkflowEngineService;
import org.jeecg.modules.innertopic.entity.InnerTopic;
import org.jeecg.modules.innertopic.service.IInnerTopicService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.task.entity.*;
import org.jeecg.modules.task.service.*;
import org.jeecg.modules.workflow.entity.WorkFlowBase;
import org.jeecg.modules.workflow.entity.WorkFlowBaseAssign;
import org.jeecg.modules.workflow.entity.WorkFlowParentNode;
import org.jeecg.modules.workflow.service.IWorkFlowBaseAssignService;
import org.jeecg.modules.workflow.service.IWorkFlowBaseService;
import org.jeecg.modules.workflow.service.IWorkFlowParentNodeService;
import org.jeecg.util.SeqGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Description: 任务统计信息
 * @author: wufanxin
 * @date: 2021-07-14
 * @version: V1.0
 */
@Slf4j
@RestController
@RequestMapping("/task/statics")
@Api(tags = "任务统计信息")
public class TaskStaticsController extends JeecgController<WorkFlowBase, IWorkFlowBaseService> {

	@Autowired
	private ITaskInfoBaseService taskInfoBaseService;

	@Autowired
	ITaskWorkflowStepService stepService;
	/**
	 * 首页数据统计
	 * @param req
	 * @return
	 */
	@ApiOperation(value = "任务统计信息-首页数据统计", notes = "任务统计信息-首页数据统计")
	@GetMapping(value = "/indexData")
	public Result<?> indexData(HttpServletRequest req) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		//查询时间范围内所有任务
		QueryWrapper<TaskInfoBase> queryWrapper =  new QueryWrapper<>();
		int total = taskInfoBaseService.count(queryWrapper);
		queryWrapper.eq("state","0");
		int unFinished = taskInfoBaseService.count(queryWrapper);
		queryWrapper =  new QueryWrapper<>();
		queryWrapper.eq("state","1");
		int finished = taskInfoBaseService.count(queryWrapper);
		queryWrapper =  new QueryWrapper<>();
		queryWrapper.eq("state","0");
		queryWrapper.lt("end_time",new Date());
		int overTime = taskInfoBaseService.count(queryWrapper);

		QueryWrapper<TaskWorkflowStep> stepQueryWrapper = new QueryWrapper<>();
		stepQueryWrapper.eq("deal_user_id",loginUser.getId());
		stepQueryWrapper.groupBy("task_no");
		List involvedList = stepService.list(stepQueryWrapper);

		Map<String,Object> ret = new HashMap<>();
		ret.put("total",total);
		ret.put("unFinished",unFinished);
		ret.put("finished",finished);
		ret.put("overTime",overTime);
		ret.put("involved",involvedList.size());
		return Result.ok(ret);
	}




}
